<template>
	<view ref="container">
		<!-- <div class="coupon-list" v-if="couponsList.length > 0">
      <div
        class="item acea-row row-center-wrapper"
        v-for="(item, index) in couponsList"
        :key="index"
      >
        <div class="money" :class="item.isUse ? 'moneyGray' : ''" :style="{backgroundImage:'http://mall-image.hxyrfwpt.com/static/images/coupon1.png'}">
          <div>
            ￥
            <span class="num">{{ item.couponPrice }}</span>
          </div>
          <div class="pic-num">满{{ item.useMinPrice }}元可用</div>
        </div>
        <div class="text">
          <div class="condition line1">
            <span class="line-title bg-color-check" v-if="item.ctype === 0">通用劵</span>
            <span class="line-title bg-color-check" v-else-if="item.ctype === 1">商品券</span>
            <span class="line-title bg-color-check" v-else>未知</span>
            <span>{{ item.cname }}</span>
          </div>
          <div class="data acea-row row-between-wrapper">
            <div v-if="item.endTime !== 0">{{ item.startTime }}-{{ item.endTime }}</div>
            <div v-else>不限时</div>
            <div class="bnt gray" v-if="item.isUse === true">已领取</div>
            <div class="bnt gray" v-else-if="item.isUse === 2">已领完</div>
            <div class="bnt bg-color-red" v-else @click="getCoupon(item.id, index)">立即领取</div>
          </div>
        </div>
      </div>
    </div> -->
		<view class="quan" v-if="couponsList.length > 0">
			<view class="quan-q" v-for="(item, index) in couponsList" :key="index">
				<view class="quan-w">
					<view class="quan-e">
						<view class="quan-r"><image src="../../../../static/img/qian.png" mode=""></image></view>
						<view class="quan-t">{{ item.couponPrice }}</view>
						<view>
							<view class="quan-y">{{ item.cname }}</view>
							<view class="quan-u">满{{ item.useMinPrice }}元可用</view>
							<view class="quan-uu">{{ item.startTime }}-{{ item.endTime }}</view>
						</view>
					</view>
					
					<view class="quan-ii" v-if="item.isUse === true || item.isUse === 2" @click="getCoupon(item.id, index,item.productIds)"></view>
					<view class="quan-i" @click="getCoupon(item.id, index,item.productIds)" v-else></view>
				</view>
			</view>
		</view>
		<Loading :loaded="loadend" :loading="loading"></Loading>
		<!--暂无优惠券-->
		<view class="noCommodity" v-if="couponsList.length === 0 && page > 1">
			<view class="noPictrue"><image src="http://mall-image.hxyrfwpt.com/static/images/noCoupon.png" class="image" /></view>
		</view>
	</view>
</template>
<script>
import { getCoupon, getCouponReceive } from '@/api/user';
import Loading from '@/components/Loading';
import DataFormatT from '@/components/DataFormatT';
import { VUE_APP_RESOURCES_URL } from '@/config';
export default {
	name: 'getCoupon',
	components: {
		Loading,
		DataFormatT
	},
	props: {},
	data: function() {
		return {
			page: 1,
			limit: 10,
			couponsList: [],
			loading: false,
			loadend: false,
			RESOURCES_URL: VUE_APP_RESOURCES_URL
		};
	},
	mounted: function() {
		this.getUseCoupons();
	},
	onReachBottom() {
		!this.loading && this.getUseCoupons();
	},
	methods: {
		getCoupon: function(id, index,productIds) {
			let that = this;
			let list = that.couponsList;
			getCouponReceive(id)
				.then(function(res) {
					list[index].isUse = true;
					that.$yrouter.push({
						path: '/pages/shop/GoodsCon/index',
						query: {
							id: productIds
						}
					});
					uni.showToast({
						title: '领取成功',
						icon: 'success',
						duration: 2000
					});
				})
				.catch(function(err) {
					uni.showToast({
						title: err.msg ,
						icon: 'none',
						duration: 2000
					});
				});
		},
		getUseCoupons: function() {
			let that = this;
			if (that.loading) return; //阻止下次请求（false可以进行请求）；
			if (that.loadend) return; //阻止结束当前请求（false可以进行请求）；
			that.loading = true;
			let q = { page: that.page, limit: that.limit, type: 3 };
			getCoupon(q).then(res => {
				that.loading = false;
				//apply();js将一个数组插入另一个数组;
				that.couponsList.push.apply(that.couponsList, res.data);
				that.loadend = res.data.length < that.limit; //判断所有数据是否加载完成；
				that.page = that.page + 1;
			});
		},
	}
};
</script>
<style lang="less" scoped>
.quan {
	.quan-q {
		width: 94%;
		height: 333rpx;
		margin: 24rpx auto;
		background-image: url('../../../../static/img/quan.png');
		background-size: 100% 100%;
		.quan-w {
			width: 70%;
			margin: auto;
			border: 0;
			.quan-e {
				display: flex;
				.quan-r {
					width: 64rpx;
					height: 64rpx;
					margin-top: 50rpx;
					image {
						width: 100%;
						height: 100%;
					}
				}
				.quan-t {
					font-size: 166rpx;
					color: #eb3e40;
					line-height: 230rpx;
					margin-left: 10rpx;
				}
				.quan-y {
					font-size: 30rpx;
					color: #eb3e40;
					margin-left: 24rpx;
					font-weight: 600;
					margin-top: 60rpx;
					overflow: hidden;
					text-overflow: ellipsis;
					display: -webkit-box;
				}
				.quan-u {
					font-size: 26rpx;
					color: #eb3e40;
					margin-top: 14rpx;
					margin-left: 24rpx;
					font-weight: 600;
				}
				.quan-uu {
					font-size: 22rpx;
					color: #eb3e40;
					margin-top: 14rpx;
					margin-left: 24rpx;
					font-weight: 600;
				}
			}
			.quan-i {
				width: 247rpx;
				height: 48rpx;
				margin: 14rpx auto;
				background-image: url('../../../../static/img/linqu.png');
				background-size: 100% 100%;
			}
			.quan-ii {
				width: 247rpx;
				height: 48rpx;
				margin: 14rpx auto;
				background-image: url('../../../../static/img/yiliqu.png');
				background-size: 100% 100%;
			}
		}
	}
}
</style>
